home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’93 / PageTool 1.0 / PagerPROAECalls.c < prev    next >
Text File  |  1993-06-17  |  14KB  |  418 lines

  1. /************************************************************************************
  2.  *
  3.  * PagerPROAECalls.c : PagerPRO AppleEvent calls
  4.  *
  5.  * By : Haider A. Isa
  6.  *
  7.  * © Caravelle Networks Corp.
  8.  * 
  9.  * April 15th, 1992
  10.  * 
  11.  ************************************************************************************/
  12.  
  13. #include    <AppleEvents.h>
  14. #include    <PPCToolBox.h>
  15. #include    <Strings.h>
  16. #include    <Errors.h>
  17. #include    "PagerPROAECalls.h"
  18.  
  19. /* ============================================================================== */
  20. /*                        A P P L E  E V E N T S     C A L L S                    */
  21. /* ============================================================================== */
  22.  
  23. OSErr    initPagerPROAEInterface ( portRefNum, userRefNum, targetAddress, theLocation, thePortInfo)
  24. short                portRefNum;        /* Current application port number */
  25. long                *userRefNum;    /* User ref num */
  26. AEAddressDesc        *targetAddress;    /* Target application address */
  27. LocationNameRec        *theLocation;    /* Source application location */
  28. PortInfoRec            *thePortInfo;    /* Source application portinfo */
  29. {
  30.     OSErr                retError;
  31.     Str32                theLocNBPType;
  32.     unsigned long        defUserRefNum;
  33.     Str32                userName;
  34.     PPCStartPBRec        pbStart;
  35.     TargetID            targetID;
  36.     
  37.     /* select a PagerPRO to link too */
  38.     if ( (retError = PPCBrowser ( nil, nil, false, theLocation, thePortInfo, (PPCFilterProcPtr) &ppsPPCFilter, theLocNBPType)) == noErr )
  39.     {
  40.         userName [0] = 0;
  41.         defUserRefNum = 0;
  42.         retError = GetDefaultUser ( &defUserRefNum, userName);
  43.         
  44.         pbStart.ioCompletion = nil;
  45.         pbStart.portRefNum = portRefNum;
  46.         pbStart.serviceType = ppcServiceRealTime;
  47.         pbStart.resFlag = 0;
  48.         pbStart.portName = &thePortInfo->name;
  49.         pbStart.locationName = theLocation;
  50.         pbStart.userData = nil;
  51.         pbStart.userRefNum = defUserRefNum;
  52.                 
  53.         /* try to establish a session */
  54.         retError = PPCStart ( &pbStart, false);
  55.         if ( retError == noErr && retError == guestNotAllowedErr )
  56.         {
  57.             /* save user ref num */
  58.             *userRefNum = pbStart.userRefNum;
  59.  
  60.             /* prepare target address */
  61.             targetID.sessionID = pbStart.sessRefNum;
  62.             BlockMove ( &thePortInfo->name, &targetID.name, sizeof(PPCPortRec));
  63.             BlockMove ( theLocation, &targetID.location, sizeof(LocationNameRec));
  64.             retError = AECreateDesc ( typeTargetID, (Ptr) &targetID, sizeof(TargetID), targetAddress);
  65.             
  66.             {    /* PROBLEM FIX:
  67.                    We are making the following call so we will force the 
  68.                    authentication dialog to appear here.  If we call 
  69.                    StartSecureSession the authentication dialog will be 
  70.                    displayed but it will also be displayed on the first 
  71.                    Apple event call.  This is a known problem with PPC and AE.
  72.                    We don't like this either.
  73.                 */
  74.  
  75.                 long        retValue;            /* Returned value */
  76.                 Str255        errString;            /* Returned error string */
  77.                 
  78.                 /* the fastest call to fix the above problem */
  79.                 doAddressExist ( (StringPtr) "\pany address", &retValue, targetAddress, errString);
  80.             }
  81.         }
  82.     }
  83.     
  84.     return ( retError);    
  85. }
  86.  
  87. OSErr    countAddressBookItems ( itemType, lSize, targetAddress, errString)
  88. OSType            itemType;            /* Item to count */
  89. long            *lSize;                /* Returned number of items */
  90. AEAddressDesc    *targetAddress;        /* Target application address */
  91. StringPtr        errString;            /* Returned error string */
  92. {
  93.     OSErr        retError;
  94.     
  95.     /* count the items in the AddressBook list */
  96.     retError = countListItems ( ADDRESSBOOK, itemType, lSize, targetAddress, errString);
  97.     return ( retError);
  98. }
  99.  
  100. OSErr    countSendQueueItems ( itemType, lSize, targetAddress, errString)
  101. OSType            itemType;            /* Item to count */
  102. long            *lSize;                /* Returned number of items */
  103. AEAddressDesc    *targetAddress;        /* Target application address */
  104. StringPtr        errString;            /* Returned error string */
  105. {
  106.     OSErr        retError;
  107.     
  108.     /* count the items in the SendQueue list */
  109.     retError = countListItems ( SENDQUEUESTATUS, itemType, lSize, targetAddress, errString);
  110.     return ( retError);
  111. }
  112.  
  113. OSErr    doAddressExist ( addString, retValue, targetAddress, errString)
  114. StringPtr        addString;            /* User or group name */
  115. long            *retValue;            /* Returned value */
  116. AEAddressDesc    *targetAddress;        /* Target application address */
  117. StringPtr        errString;            /* Returned error string */
  118. {
  119.     AppleEvent        theAppleEvent;
  120.     AppleEvent        reply;
  121.     AEDescList        param1List;
  122.     AEDesc            param1;
  123.     Size            actualSize;
  124.     DescType        returnedType;
  125.     OSErr            retError;
  126.     
  127.     errString [0] = 0;
  128.     reply.dataHandle = nil;
  129.     
  130.     /* create a CoreEvent, DoObjectsExist */
  131.     retError = AECreateAppleEvent ( kCoreEventClass, kAEDoObjectsExist, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, &theAppleEvent);
  132.     if ( retError == noErr )
  133.     {
  134.         /* param1: address name */
  135.         retError = AECreateList ( nil, 0, false, ¶m1List);
  136.         retError = AECreateDesc ( typeChar, (Ptr) &addString [1], addString [0], ¶m1);
  137.         retError = AEPutDesc ( ¶m1List, 0, ¶m1);
  138.         retError = AEPutParamDesc ( &theAppleEvent, keyDirectObject, ¶m1List);
  139.         
  140.         /* send the event */
  141.         retError = AESend ( &theAppleEvent, &reply, kAEWaitReply + kAENeverInteract, kAENormalPriority, 120, nil, nil);
  142.         if ( retError == noErr )
  143.         {
  144.             /* get the size */
  145.             if ( AEGetParamPtr ( &reply, keyAEResult, typeLongInteger, &returnedType, (Ptr) retValue, sizeof(long), &actualSize) != noErr )
  146.             {
  147.                 /* get the error message */
  148.                 AEGetParamPtr ( &reply, keyErrorString, typeChar, &returnedType, (Ptr) errString, 255, &actualSize);
  149.                 
  150.                 errString [actualSize] = nil;
  151.                 c2pstr ( (Ptr) errString);
  152.                 
  153.                 retError = errAEEventFailed;
  154.             }
  155.         }
  156.         
  157.         if ( param1.dataHandle )
  158.             AEDisposeDesc ( ¶m1);
  159.         if ( reply.dataHandle )
  160.             AEDisposeDesc ( &reply);
  161.     }
  162.     
  163.     return ( retError);
  164. }
  165.  
  166. OSErr    getAddressBookItem ( index, dataRecord, targetAddress, errString)
  167. long            index;                /* Item index to read */
  168. Ptr                dataRecord;            /* Returned read data */
  169. AEAddressDesc    *targetAddress;        /* Target application address */
  170. StringPtr        errString;            /* Returned error string */
  171. {
  172.     OSErr        retError;
  173.     
  174.     /* read an item form the AddressBook list */
  175.     retError = getListItem ( ADDRESSBOOK, index, dataRecord, sizeof(addressBookData), targetAddress, errString);
  176.     return ( retError);
  177. }
  178.  
  179. OSErr    getSendQueueItem ( index, dataRecord, targetAddress, errString)
  180. long            index;                /* Item index to read */
  181. Ptr                dataRecord;            /* Returned read data */
  182. AEAddressDesc    *targetAddress;        /* Target application address */
  183. StringPtr        errString;            /* Returned error string */
  184. {
  185.     OSErr        retError;
  186.     
  187.     /* read an item from the SendQueue list */
  188.     retError = getListItem ( SENDQUEUESTATUS, index, dataRecord, sizeof(sendQueueData), targetAddress, errString);
  189.     return ( retError);
  190. }
  191.  
  192. OSErr    sendMessage ( msgPriority, addString, msgString, targetAddress, errString)
  193. OSType            msgPriority;        /* message priority */
  194. StringPtr        addString;            /* User or Group name */
  195. StringPtr        msgString;            /* Message */
  196. AEAddressDesc    *targetAddress;        /* Target application address */
  197. StringPtr        errString;            /* Returned error string */
  198. {
  199.     AppleEvent        theAppleEvent;
  200.     AppleEvent        reply;
  201.     AEDescList        param1List;
  202.     AEDesc            param1;
  203.     AEDescList        param2List;
  204.     AEDesc            param2;
  205.     AEDescList        param3List;
  206.     AEDesc            param3;
  207.     Size            actualSize;
  208.     DescType        returnedType;
  209.     OSErr            retError;
  210.     
  211.     errString [0] = 0;
  212.     reply.dataHandle = nil;
  213.     
  214.     /* create a CoreEvent, SetData */
  215.     retError = AECreateAppleEvent ( kCoreEventClass, kAESetData, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, &theAppleEvent);
  216.     if ( retError == noErr )
  217.     {
  218.         /* param1: priority */
  219.         retError = AECreateList ( nil, 0, false, ¶m1List);
  220.         retError = AECreateDesc ( typeChar, (Ptr) &msgPriority, sizeof(OSType), ¶m1);
  221.         retError = AEPutDesc ( ¶m1List, 0, ¶m1);
  222.         retError = AEPutParamDesc ( &theAppleEvent, keyDirectObject, ¶m1List);
  223.         
  224.         /* param2: address name */
  225.         retError = AECreateList ( nil, 0, false, ¶m2List);
  226.         retError = AECreateDesc ( typeChar, (Ptr) &addString [1], addString [0], ¶m2);
  227.         retError = AEPutDesc ( ¶m2List, 0, ¶m2);
  228.         retError = AEPutParamDesc ( &theAppleEvent, keyAEName, ¶m2List);
  229.             
  230.         /* param3: message */
  231.         retError = AECreateList ( nil, 0, false, ¶m3List);
  232.         retError = AECreateDesc ( typeChar, (Ptr) &msgString [1], msgString [0], ¶m3);
  233.         retError = AEPutDesc ( ¶m3List, 0, ¶m3);
  234.         retError = AEPutParamDesc ( &theAppleEvent, keyAEData, ¶m3List);
  235.         
  236.         /* send the event */
  237.         retError = AESend ( &theAppleEvent, &reply, kAEWaitReply + kAENeverInteract, kAENormalPriority, 120, nil, nil);
  238.         if ( retError == noErr )
  239.         {
  240.             /* get the error message */
  241.             if ( AEGetParamPtr ( &reply, keyErrorString, typeChar, &returnedType, (Ptr) errString, 255, &actualSize) == noErr )
  242.             {
  243.                 errString [actualSize] = nil;
  244.                 c2pstr ( (Ptr) errString);
  245.                 
  246.                 retError = errAEEventFailed;
  247.             }
  248.         }
  249.         
  250.         if ( param1.dataHandle )
  251.             AEDisposeDesc ( ¶m1);
  252.         if ( param2.dataHandle )
  253.             AEDisposeDesc ( ¶m2);
  254.         if ( param3.dataHandle )
  255.             AEDisposeDesc ( ¶m3);
  256.         if ( reply.dataHandle )
  257.             AEDisposeDesc ( &reply);
  258.     }
  259.     
  260.     return ( retError);
  261. }
  262.  
  263. /* ============================================================================== */
  264. /*                            S U P P O R T     C A L L S                         */
  265. /* ============================================================================== */
  266.  
  267. OSErr    countListItems ( listType, itemType, lSize, targetAddress, errString)
  268. OSType            listType;            /* List to look in */
  269. OSType            itemType;            /* Item to count */
  270. long            *lSize;                /* Returned number of items */ 
  271. AEAddressDesc    *targetAddress;        /* Target application address */
  272. StringPtr        errString;            /* Returned error string */
  273. {
  274.     AppleEvent        theAppleEvent;
  275.     AppleEvent        reply;
  276.     AEDescList        param1List;
  277.     AEDesc            param1;
  278.     AEDescList        param2List;
  279.     AEDesc            param2;
  280.     Size            actualSize;
  281.     DescType        returnedType;
  282.     OSErr            retError;
  283.     
  284.     errString [0] = 0;
  285.     *lSize = 0;
  286.     reply.dataHandle = nil;
  287.     
  288.     /* create a CoreEvent, CountObjectElements */
  289.     retError = AECreateAppleEvent ( kCoreEventClass, kAECountElements, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, &theAppleEvent);
  290.     if ( retError == noErr )
  291.     {
  292.         /* param1: listType */
  293.         retError = AECreateList ( nil, 0, false, ¶m1List);
  294.         retError = AECreateDesc ( typeChar, (Ptr) &listType, sizeof(OSType), ¶m1);
  295.         retError = AEPutDesc ( ¶m1List, 0, ¶m1);
  296.         retError = AEPutParamDesc ( &theAppleEvent, keyDirectObject, ¶m1List);
  297.         
  298.         /* param2: itemType */
  299.         retError = AECreateList ( nil, 0, false, ¶m2List);
  300.         retError = AECreateDesc ( typeChar, (Ptr) &itemType, sizeof(OSType), ¶m2);
  301.         retError = AEPutDesc ( ¶m2List, 0, ¶m2);
  302.         retError = AEPutParamDesc ( &theAppleEvent, keyAEObjectClass, ¶m2List);
  303.         
  304.         /* send the event */
  305.         retError = AESend ( &theAppleEvent, &reply, kAEWaitReply + kAENeverInteract, kAENormalPriority, 120, nil, nil);
  306.         if ( retError == noErr )
  307.         {
  308.             /* get the size */
  309.             if ( AEGetParamPtr ( &reply, keyAEResult, typeLongInteger, &returnedType, (Ptr) lSize, sizeof(long), &actualSize) != noErr )
  310.             {
  311.                 /* get the error message */
  312.                 AEGetParamPtr ( &reply, keyErrorString, typeChar, &returnedType, (Ptr) errString, 255, &actualSize);
  313.                 
  314.                 errString [actualSize] = nil;
  315.                 c2pstr ( (Ptr) errString);
  316.                 
  317.                 retError = errAEEventFailed;
  318.             }
  319.         }
  320.         
  321.         if ( param1.dataHandle )
  322.             AEDisposeDesc ( ¶m1);
  323.         if ( param2.dataHandle )
  324.             AEDisposeDesc ( ¶m2);
  325.         if ( reply.dataHandle )
  326.             AEDisposeDesc ( &reply);
  327.     }
  328.     
  329.     return ( retError);
  330. }
  331.  
  332. OSErr    getListItem ( listType, index, dataRecord, dataSize, targetAddress, errString)
  333. OSType            listType;            /* List to read from */
  334. long            index;                /* Item index to read */
  335. Ptr                dataRecord;            /* Returned data */
  336. short            dataSize;            /* Requested data size */
  337. AEAddressDesc    *targetAddress;        /* Target application address */
  338. StringPtr        errString;            /* Returned error string */
  339. {
  340.     AppleEvent        theAppleEvent;
  341.     AppleEvent        reply;
  342.     AEDescList        param1List;
  343.     AEDesc            param1;
  344.     AEDescList        param2List;
  345.     AEDesc            param2;
  346.     Size            actualSize;
  347.     DescType        returnedType;
  348.     OSErr            retError;
  349.     
  350.     errString [0] = 0;
  351.     reply.dataHandle = nil;
  352.     
  353.     /* create a CoreEvent, GetData */
  354.     retError = AECreateAppleEvent ( kCoreEventClass, kAEGetData, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, &theAppleEvent);
  355.     if ( retError == noErr )
  356.     {
  357.         /* param1: listType */
  358.         retError = AECreateList ( nil, 0, false, ¶m1List);
  359.         retError = AECreateDesc ( typeChar, (Ptr) &listType, sizeof(OSType), ¶m1);
  360.         retError = AEPutDesc ( ¶m1List, 0, ¶m1);
  361.         retError = AEPutParamDesc ( &theAppleEvent, keyDirectObject, ¶m1List);
  362.         
  363.         /* param2: index */
  364.         retError = AECreateList ( nil, 0, false, ¶m2List);
  365.         retError = AECreateDesc ( typeInteger, (Ptr) &index, sizeof(long), ¶m2);
  366.         retError = AEPutDesc ( ¶m2List, 0, ¶m2);
  367.         retError = AEPutParamDesc ( &theAppleEvent, keyAERequestType, ¶m2List);
  368.         
  369.         /* send the event */
  370.         retError = AESend ( &theAppleEvent, &reply, kAEWaitReply + kAENeverInteract, kAENormalPriority, 120, nil, nil);
  371.         if ( retError == noErr )
  372.         {
  373.             /* get the size */
  374.             if ( AEGetParamPtr ( &reply, keyAEResult, typeAERecord, &returnedType, dataRecord, dataSize, &actualSize) != noErr )
  375.             {
  376.                 /* get the error message */
  377.                 AEGetParamPtr ( &reply, keyErrorString, typeChar, &returnedType, (Ptr) errString, 255, &actualSize);
  378.                 
  379.                 errString [actualSize] = nil;
  380.                 c2pstr ( (Ptr) errString);
  381.                 
  382.                 retError = errAEEventFailed;
  383.             }
  384.         }
  385.         
  386.         if ( param1.dataHandle )
  387.             AEDisposeDesc ( ¶m1);
  388.         if ( param2.dataHandle )
  389.             AEDisposeDesc ( ¶m2);
  390.         if ( reply.dataHandle )
  391.             AEDisposeDesc ( &reply);
  392.     }
  393.     
  394.     return ( retError);
  395. }
  396.  
  397. pascal    Boolean    ppsPPCFilter ( theLoc, thePortInfo)
  398. LocationNamePtr        theLoc;
  399. PortInfoPtr            thePortInfo;
  400. {
  401. OSType        retType;
  402. #pragma unused (theLoc)
  403.  
  404.     /* only PagerPRO types */
  405.     if ( thePortInfo->name.portKindSelector == ppcByCreatorAndType )
  406.     {
  407.         if ( thePortInfo->name.u.port.creator == PAGERPROCREATOR )
  408.             return ( true);
  409.     }
  410.     else {
  411.         BlockMove ( &thePortInfo->name.u.portTypeStr [1], &retType, sizeof(OSType));
  412.         if ( retType == PAGERPROCREATOR )
  413.             return ( true);
  414.     }
  415.  
  416.     return ( false);
  417. }
  418.